package org.bilinc.gz001;

import org.bilinc.gz001.hibernateUtil.HibernateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;

public class ApplicationController {

    @Autowired
    HibernateUtil hibernateUtil;

    private final ModelService mModelService;
    private final UserInterface mUserInterface;

    ApplicationController(final ModelService modelService, final UserInterface view) {
        mModelService = modelService;
        mUserInterface = view;
        mUserInterface.aboveListener(new AboveSelectionListener());
        mUserInterface.belowListener(new BelowSelectionListener());
        mUserInterface.windowListener(new AppWindowListener());
        mUserInterface.mouseListener(new MouseHandler());
    }

    private class AboveSelectionListener implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            final ListSelectionModel lsm = (ListSelectionModel) e.getSource();
            int selectedLine = lsm.getMaxSelectionIndex();
            mUserInterface.selectAboveTableRow(selectedLine);
        }
    }

    private class BelowSelectionListener implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            final ListSelectionModel lsm = (ListSelectionModel) e.getSource();
            final int selectedLine = lsm.getMaxSelectionIndex();
            mUserInterface.selectBelowTableRow(selectedLine);
        }
    }

    private class AppWindowListener implements WindowListener {

        @Override
        public void windowOpened(WindowEvent e) {
        }

        @Override
        public void windowClosing(WindowEvent e) {
            mModelService.getHibernateUtil().shutdown();
            mUserInterface.onWindowClosing();
        }

        @Override
        public void windowClosed(WindowEvent e) {
        }

        @Override
        public void windowIconified(WindowEvent e) {
        }

        @Override
        public void windowDeiconified(WindowEvent e) {
        }

        @Override
        public void windowActivated(WindowEvent e) {
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
        }
    }

    private class MouseHandler extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                mUserInterface.onLeftMouseButton(e);
            } else if (SwingUtilities.isRightMouseButton(e)) {
                mUserInterface.onRightMouseButton(e);
            }
        }

        public void mousePressed(MouseEvent ev) {
            mUserInterface.onPressedMouseButton(ev);
        }

        public void mouseReleased(MouseEvent ev) {
            mUserInterface.onReleasedMouseButton(ev);
        }
    }
}
